package com.hyh.ui.localnumber;

import android.content.Intent;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.hyh.util.PermissionUtil;
import com.hyh.service.CoreService;
import com.hyh.ui.base.BaseActivity;
import com.hyh.ui.contacts.CommActivity;
import com.hyh.util.SharePreferenceUtil;
import com.hyh.videochat.R;
import com.platform.contact.UserInfo;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2019/1/26.
 */

public class LocalNumberActivity extends BaseActivity implements View.OnClickListener,View.OnTouchListener {

    @BindView(R.id.iv_icon)
    ImageView ivIcon;
    @BindView(R.id.et_local_phone_num)
    EditText etLocalPhoneNum;
    @BindView(R.id.btn_confirm)
    Button btnConfirm;
    @BindView(R.id.relative_layout)
    RelativeLayout layout;


    @Override
    public int getLayout() {
        return R.layout.activity_local_number;
    }

    @Override
    public void getIntentExtras(Intent intent) {
    }

    @Override
    public void initDataAndEvent() {
        PermissionUtil.verifyAppPermissions(this);
        Intent intent = new Intent(this, CoreService.class);
        startService(intent);
        String localNumber = SharePreferenceUtil.getString(SharePreferenceUtil.Contact.LocalNumber);
        if(localNumber!=null){
            UserInfo.id = localNumber;
            CommActivity.startActivity(this);
            finish();
        }
        Animation animation = AnimationUtils.loadAnimation(this,R.anim.rotate_image);
        ivIcon.setAnimation(animation);
        layout.setOnTouchListener(this);
    }

    @OnClick({R.id.iv_icon,R.id.btn_confirm})
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_icon:
                ShowImgActivity.startActivity(this,R.drawable.address_book_father_big);
                break;
            case R.id.btn_confirm:
                String localNumber = etLocalPhoneNum.getText().toString().trim();
                if(localNumber.length()<11){
                    Toast.makeText(this,"手机号码格式不正确",Toast.LENGTH_SHORT).show();
                    return;
                }
                UserInfo.id = localNumber;
                SharePreferenceUtil.save(SharePreferenceUtil.Contact.LocalNumber,localNumber);
                CommActivity.startActivity(this);
                finish();
                break;
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction()==MotionEvent.ACTION_DOWN){
            InputMethodManager imm = (InputMethodManager) getSystemService(this.INPUT_METHOD_SERVICE);
            if (imm != null) {
                imm.hideSoftInputFromWindow(etLocalPhoneNum.getWindowToken(), 0);
            }
        }
        return false;
    }
}
